package com.haoe.util;

import com.google.common.collect.Lists;

import java.sql.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 80305082
 * @Date: 2021/4/12
 * @Description: 获取存在于UAT环境但是不存在于DEV环境的数据库表
 */
public class ExsistUATButNotDEVTables {
    private static Connection conn;

    private static List<String> inDEVTables() {
        DBinitdev();
        List<String> result = Lists.newArrayList();
        try {
            //获取数据库的元数据
            DatabaseMetaData dbMetaData = conn.getMetaData();
            //从元数据中获取到所有的表名
            ResultSet rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});
            //存放所有表名
            List<String> tableNames = Lists.newArrayList();
            //存放当前表的字段
            //List<String> fields = Lists.newArrayList();
            //存放当前表的字段类型
            //List<String> fieldstype = Lists.newArrayList();
            while (rs.next()) {
                String tableName = rs.getString("TABLE_NAME");
                //System.out.println("表名: "+tableName);
                //System.out.println("表类型: "+rs.getString("TABLE_TYPE"));
                //System.out.println("表所属数据库: "+rs.getString("TABLE_CAT"));
                //System.out.println("表所属用户名: "+rs.getString("TABLE_SCHEM"));
                //System.out.println("表备注: "+rs.getString("REMARKS"));
                tableNames.add(tableName);
            }
            result.addAll(tableNames);
            //查询每个表的字段
            //for(String record:tableNames)
            //{
            //    String sql = "select * from " + record;
            //    PreparedStatement ps = conn.prepareStatement(sql);
            //    ResultSet rstable = ps.executeQuery();
            //    //结果集元数据
            //    ResultSetMetaData meta = rstable.getMetaData();
            //    //表列数量
            //    int columeCount=meta.getColumnCount();
            //    for (int i=1;i<=columeCount;i++)
            //    {
            //        fields.add(meta.getColumnName(i));
            //        fieldstype.add(meta.getColumnTypeName(i));
            //
            //    }
            //    System.out.println("表"+record+"字段: "+fields);
            //    System.out.println("表"+record+"字段类型: "+fieldstype);
            //    fields.clear();
            //}
        } catch (SQLException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        DBclose();
        return result;
    }

    private static List<String> inUATTables() {
        DBinituat();
        List<String> result = Lists.newArrayList();
        try {
            //获取数据库的元数据
            DatabaseMetaData dbMetaData = conn.getMetaData();
            //从元数据中获取到所有的表名
            ResultSet rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});
            //存放所有表名
            List<String> tableNames = Lists.newArrayList();

            while (rs.next()) {
                String tableName = rs.getString("TABLE_NAME");

                tableNames.add(tableName);
            }
            result.addAll(tableNames);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBclose();
        return result;
    }

    private static void DBinitdev() {
        //驱动程序名
        //String driver = "com.mysql.jdbc.Driver";
        //MySQL配置时的用户名
        String user = "fireAnt";
        //MySQL配置时的密码
        String password = "Fireant@2019";
        //不同端口号
        //String[] db_url_port = {"32080", "32081", "32082","3308"};
        //数据库服务器
        //for(String db_port:db_url_port)
        //{
        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://55.14.76.103:3306/fireant";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //声明Connection对象
            conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed()) {
                System.out.println("当前访问数据库端口号为:3306，数据库连接成功！");
                //break;
            }
        } catch (Exception e) {
            System.out.println("当前访问数据库端口号为:3306，数据库连接失败！");
            //continue;
        }
        //}
    }

    private static void DBinituat() {
        String user = "fireAnt";
        //MySQL配置时的密码
        String password = "Fireant@2019";
        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://55.13.131.43:3306/fa_uat";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //声明Connection对象
            conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed()) {
                System.out.println("当前访问数据库端口号为:3306，数据库连接成功！");
            }
        } catch (Exception e) {
            System.out.println("当前访问数据库端口号为:3306，数据库连接失败！");
        }
    }

    private static void DBclose() {
        try {
            conn.close();
        } catch (SQLException e) {
            //TODO Auto-generated catch block
            System.out.println("数据关闭异常");
            e.printStackTrace();
        }
    }

    private static List<String> inUATButNotInDEV() {
        List<String> inUATTables = inUATTables();
        List<String> inDEVTables = inDEVTables();
        List<String> result = inUATTables.stream().filter(item -> !inDEVTables.contains(item)).collect(Collectors.toList());
        return result;

    }

    public static void main(String[] args) {
        List<String> inDEVTables = inDEVTables();
        System.out.println(inDEVTables);
        System.out.println("Dev环境共计" + inDEVTables.size() + "张表");
        System.out.println("=============================");
        List<String> inUATTables = inUATTables();
        System.out.println(inUATTables);
        System.out.println("UAT环境共计" + inUATTables.size() + "张表");

        List<String> inUATButNotInDEV = inUATButNotInDEV();
        System.out.println(inUATButNotInDEV);

    }
}
